package qdu.com.servlet;


import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hdfs.FileUtil;
import qdu.com.bean.seasonBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@WebServlet("/yearseason")
public class yearSeasonServlet extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        Map<Integer,String[]> map = FileUtil.ReadPartFile(getServletContext().getRealPath("/files")+"/part-r-00002");

        String season="";

        List<seasonBean> list = new ArrayList<>();
        Iterator it = map.values().iterator();//使用现成的Iterator ：map.values().iterator
        while (it.hasNext()) {
            String[] w= (String[]) it.next();
            switch (w[1]){
                case  "1": season=w[0]+"-春";break;
                case  "2": season=w[0]+"-夏";break;
                case  "3": season=w[0]+"-秋";break;
                case  "4": season=w[0]+"-冬";break;
            }
            list.add(new seasonBean(season,w[2],w[3],w[4]));
        }
        String json = mapper.writeValueAsString(list);
        System.out.println(json);
        System.out.println(".......");
        System.out.println(getServletContext().getRealPath("/files"));
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write(json);
    }
}
